package com.zl.zlbaseproject.widget;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zl.zlbaseproject.R;


/**
 * Created by 郑磊 on 2018/5/27.
 *
 * loading empty success  error  布局控制器
 */

public class LayoutStatusManager  {

    private Context mContext;



    public static final byte STATUS_LOADING = 0;
    public static final byte STATUS_ERROR = 1;
    public static final byte STATUS_SUCCESS=2 ;
    public static final byte STATUS_EMPTY= 3;


    //加载中布局
    @LayoutRes
    private int mLoadingLayout;
    private View mLoadingView;
    private String mLoadingText;

    //成功布局
    @LayoutRes
    private int mSuccessLayout;
    private View mSuccessView;

    //失败布局
    @LayoutRes
    private int mErrordLayout;
    private View mErrorView;
    private String mErrordText;

    //空布局
    @LayoutRes
    private int mEmptyLayout;
    private View mEmptyView;
    private String mEmptyText;

    //当前布局

    private View mCurrentView;
    /**
     * contentLayout 的布局参数
     */
    private ViewGroup.LayoutParams params;

    //打气筒
    private LayoutInflater mInflater;

    /**
     * mParentLayout 的父 ViewGroup
     */
    private ViewGroup mParentLayout;
    private int mIndex;

    public LayoutStatusManager(@NonNull Context context,View successView) {
        mContext =context;
        this.mSuccessView =successView;
        init();

    }




    private void init() {
        getContentLayoutParams();
        mLoadingView = inflate(R.layout.default_loading);//  默认的加载中布局
        mErrorView =inflate(R.layout.default_error);
        mEmptyView =inflate(R.layout.default_empty);
    }


    private void getContentLayoutParams() {
        this.params = mSuccessView.getLayoutParams();
        if (mSuccessView.getParent() != null) {
            mParentLayout= (ViewGroup) mSuccessView.getParent();
        } else {
            mParentLayout = mSuccessView.getRootView().findViewById(android.R.id.content);
        }

        int count = mParentLayout.getChildCount();
        for (int index = 0; index < count; index++) {
            if (mSuccessView == mParentLayout.getChildAt(index)) {
                // 获取 contentLayout 在 parentLayout 中的位置
                mIndex = index;
                break;
            }
        }
    }

    private View inflate(@LayoutRes int resource) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(mContext);
        }
        return mInflater.inflate(resource, null);
    }


    /////////////////默认布局////////////////////



    /**
     * 设置加载中布局
     *
     */
    public void setLoadingLayout(@NonNull View loadingView) {
        this.mLoadingView = loadingView;
    }
    public void setLoadingRes(int loadingRes){
        this.mLoadingView = inflate(loadingRes);
    }


    /**
     * 设置空布局
     *
     */
    public void setEmptyLayout(@NonNull View emptyView) {
        this.mEmptyView = emptyView;
    }
    public void setEmptyRes(int emptyRes){
        this.mEmptyView = inflate(emptyRes);
    }


    /**
     * 设置错误布局
     *
     */
    public void setErrorLayout(@NonNull View EmptyView) {
        this.mErrorView = EmptyView;
    }
    public void setErrorRes(int errorRes){
        this.mErrorView = inflate(errorRes);
    }

    public void  setStatus(byte status)  {

        if(status == STATUS_LOADING){
            showStatusLayout(mLoadingView);
        }else if(status == STATUS_ERROR){
            showStatusLayout(mErrorView);
        }else if(status == STATUS_EMPTY){
            showStatusLayout(mEmptyView);
        }else if(status == STATUS_SUCCESS){
            showStatusLayout(mSuccessView);
        }

    }

    private void showStatusLayout(View view) {
        // 如果当前显示的布局不是 view，才进行替换
        if (view == null) {
            return ;
        }
        if (mCurrentView != view) {
            mCurrentView = view;
            ViewGroup parent = (ViewGroup) view.getParent();
            // 去除 view 的 父 view，才能添加到别的 ViewGroup 中
            if (parent != null) {
                parent.removeView(view);
            }
            // 替换 = 移除 + 添加
            mParentLayout.removeViewAt(mIndex);
            mParentLayout.addView(view, mIndex, params);
        }
    }

}
